﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using b7.Models;

namespace b7.Controllers
{
    //[Authorize(Roles = "adminshop,admin")]
    public class SanphamController : Controller
    {
        private b7Models db = new b7Models();

        //
        // GET: /Sanpham/

        public ViewResult Index()
        {
            var sanpham = db.Sanphams.Include(a => a.NhomSanpham).Include(a=>a.Member).Include(a=>a.Theloai);
            return View(sanpham.ToList());
        }

        //
        // GET: /Sanpham/Details/5

        public ViewResult Details(int id)
        {
            Sanpham sanpham = db.Sanphams.Find(id);
            return View(sanpham);
        }

        //
        // GET: /Sanpham/Create

        public ActionResult Create()
        {
            ViewBag.NhomSanphamId = new SelectList(db.NhomSanphams, "NhomSanphamId", "Name");
            ViewBag.TheloaiId = new SelectList(db.Theloais, "TheloaiId", "Name");
            ViewBag.MemberId = new SelectList(db.Members, "MemberId", "UserName");
            //Member mb = new Member();
            //mb.MemberId = User.Identity.
            return View();
        } 

        //
        // POST: /Sanpham/Create

        [HttpPost]
        public ActionResult Create(Sanpham sanpham)
        {
            if (ModelState.IsValid)
            {
                db.Sanphams.Add(sanpham);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            ViewBag.NhomSanphamId = new SelectList(db.NhomSanphams, "NhomSanphamId", "Name",sanpham.NhomSanphamId);
            ViewBag.TheloaiId = new SelectList(db.Theloais, "TheloaiId", "Name",sanpham.TheloaiId);
            ViewBag.MemberId = new SelectList(db.Members, "MemberId", "UserName",sanpham.MemberId);

            return View(sanpham);
        }
        
        //
        // GET: /Sanpham/Edit/5
 
        public ActionResult Edit(int id)
        {
            ViewBag.NhomSanphamId = new SelectList(db.NhomSanphams, "NhomSanphamId", "Name");
            ViewBag.TheloaiId = new SelectList(db.Theloais, "TheloaiId", "Name");
            ViewBag.MemberId = new SelectList(db.Members, "MemberId", "UserName");
            Sanpham sanpham = db.Sanphams.Find(id);
            return View(sanpham);
        }

        //
        // POST: /Sanpham/Edit/5

        [HttpPost]
        public ActionResult Edit(Sanpham sanpham)
        {
            if (ModelState.IsValid)
            {
                db.Entry(sanpham).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.NhomSanphamId = new SelectList(db.NhomSanphams, "NhomSanphamId", "Name");
            ViewBag.TheloaiId = new SelectList(db.Theloais, "TheloaiId", "Name");
            ViewBag.MemberId = new SelectList(db.Members, "MemberId", "UserName");
            return View(sanpham);
        }

        //
        // GET: /Sanpham/Delete/5
 
        public ActionResult Delete(int id)
        {
            Sanpham sanpham = db.Sanphams.Find(id);
            return View(sanpham);
        }

        //
        // POST: /Sanpham/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Sanpham sanpham = db.Sanphams.Find(id);
            db.Sanphams.Remove(sanpham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        public ActionResult Search()
        {
            ViewBag.NhomSanphamId = new SelectList(db.NhomSanphams, "NhomSanphamId", "Name");
            return PartialView("_Search");
        }
    }
}